package com.aizhixin.lab.docs.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.docs.dto.DocContentDTO;
import com.aizhixin.lab.docs.entity.DocContent;
import com.aizhixin.lab.docs.entity.DocMenu;
import com.aizhixin.lab.docs.repository.DocContentRepository;
import com.aizhixin.lab.docs.repository.DocMenuRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class HelpDocService {
    @Autowired
    private DocMenuRepository docMenuRepository;
    @Autowired
    private DocContentRepository docContentRepository;

    public List getMenuList(Integer type) {
        return docMenuRepository.findByTypeAndParentIdIsNullOrderByOrderNo(type);
    }

    public Map<String, Object> saveMenu(String id, String name, String parentId, Integer type, String menuNo) {
        DocMenu menu = null;
        if (!StringUtils.isEmpty(id)) {
            menu = docMenuRepository.findOne(id);
        }
        if (menu == null) {
            menu = new DocMenu();
            menu.setId(UUID.randomUUID().toString());
        }
        menu.setName(name);
        menu.setParentId(parentId);
        menu.setType(type);
        menu.setMenuNo(menuNo);
        String no = menuNo.replaceAll("\\.", "");
        menu.setOrderNo(Integer.parseInt(no));
        docMenuRepository.save(menu);
        Map<String, Object> result = new HashMap<>();
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.ID, menu.getId());
        return result;
    }

    public Map<String, Object> saveDoc(DocContentDTO dto) {
        Map<String, Object> result = new HashMap<>();
        DocMenu menu = docMenuRepository.findOne(dto.getMenuId());
        if (menu != null) {
            DocContent content = docContentRepository.findByMenuId(dto.getMenuId());
            if (content == null) {
                content = new DocContent();
                content.setId(UUID.randomUUID().toString());
                content.setMenuId(dto.getMenuId());
            }
            content.setContent(dto.getContent());
            docContentRepository.save(content);
            result.put(ApiReturnConstants.SUCCESS, true);
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
        }
        return result;
    }

    public Map<String, Object> delete(String menuId) {
        Map<String, Object> result = new HashMap<>();
        DocMenu menu = docMenuRepository.findOne(menuId);
        if (menu != null) {
            docMenuRepository.delete(menu);
            result.put(ApiReturnConstants.SUCCESS, true);
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
        }
        return result;
    }

    public DocContent getDoc(String menuId) {
        return docContentRepository.findByMenuId(menuId);
    }

}
